<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/7
 * Time: 14:25
 */

namespace zhtfrontend\controllers;


use common\components\helpers\Tree;
use common\models\datas\Cate;
use doumiaoduo\buildform\BuildForm;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\CateRelation;
use zhtfrontend\models\datas\Classroom;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use zhtfrontend\models\datas\ClassroomGroup;
use zhtfrontend\models\datas\Mechanic;
use yii\data\Pagination;
use yii\helpers\Json;

class ClassroomgroupController extends BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;

        $classroom = ClassroomGroup::find()->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['name'])) {
                $classroom->andWhere(['like', 'name', $search_data['name']]);
            }
        }
        $pages = new Pagination(['totalCount' => $classroom->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $classroom = $classroom->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($classroom, ['operate']);
        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }

    /*
     * 教室添加
     */
    public function actionCreate()
    {
        $this->layout = false;
        $model = new ClassroomGroup();
        $form = new BuildForm('@app/components/forms/classroomgroupForm.php');
        $form->loadModel(['ClassroomGroup' => $model]);
        $form->loadDefaultValues();
        $form->setItems('ClassroomGroup', 'type', [1 => '益智', 2 => '音乐', 3 => '形体']);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->user_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['classroomgroup/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    /*
     * 教室修改
     */
    public function actionUpdate()
    {
        $this->layout = false;
        $id = \Yii::$app->request->get('id');
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = ClassroomGroup::findOne($id);
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        $form = new BuildForm('@app/components/forms/classroomgroupForm.php');
        $form->loadModel(['ClassroomGroup' => $model]);
        $form->loadDefaultValues();
        $form->setItems('ClassroomGroup', 'type', [1 => '益智', 2 => '音乐', 3 => '形体']);
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['classroomgroup/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionDelete()
    {
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = ClassroomGroup::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        if ($model->delete()) {
            return $this->redirectMessage(\Yii::t('zhtfrontend', '信息删除成功'), Fun::url(['classroomgroup/index']));
        }
    }
}
